<!DOCTYPE html>
<html>
<head>
    <title>新增用户</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item" id="openTypeItem">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <select name="type" id="openType" lay-filter="oneLeverChange">
                            <option value="1" selected="selected">单选提</option>
                            <option value="2">多选题</option>
                            <option value="3">判断题</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="title">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title"  autocomplete="off" placeholder="请输入标题"
                               class="layui-input" >
                    </div>
                </div>
                  <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort" value="1"  autocomplete="off" placeholder="请输入排序"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="A">
                    <label class="layui-form-label">A</label>
                    <div class="layui-input-block">
                        <input type="text" name="A"   autocomplete="off" placeholder="请输入答案A"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="B">
                    <label class="layui-form-label">B</label>
                    <div class="layui-input-block">
                        <input type="text" name="B"   autocomplete="off" placeholder="请输入答案B"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="C">
                    <label class="layui-form-label">C</label>
                    <div class="layui-input-block">
                        <input type="text" name="C"   autocomplete="off" placeholder="请输入答案C"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" id="D">
                    <label class="layui-form-label">D</label>
                    <div class="layui-input-block">
                        <input type="text" name="D"  autocomplete="off" placeholder="请输入答案D"
                               class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item layui-form-text" id="answer1">
                    <label class="layui-form-label">答案</label>
                    <div class="layui-input-block">
                      <input type="radio" name="answer1" value="1"  title="A" checked="">
                      <input type="radio" name="answer1" value="2"  title="B">
                      <input type="radio" name="answer1" value="3" title="C" >
                      <input type="radio" name="answer1" value="4" title="D">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" id="answer2" display="none">
                    <label class="layui-form-label">答案</label>
                    <div class="layui-input-block">
                      <input type="checkbox" name="answer2" value="1" lay-skin="primary"  title="A" checked="">
                      <input type="checkbox" name="answer2" value="2" lay-skin="primary" title="B" >
                      <input type="checkbox" name="answer2" value="3"lay-skin="primary"  title="C">
                      <input type="checkbox" name="answer2" value="4" lay-skin="primary"  title="D">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text" id="answer3">
                    <label class="layui-form-label">答案</label>
                    <div class="layui-input-block">
                      <input type="radio" name="answer3" value="1"  title="正确" checked="">
                      <input type="radio" name="answer3" value="2" title="错误" >
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="dept-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}

<script>
  layui.use(['form', 'jquery', 'dtree'], function () {
    let form = layui.form
    let $ = layui.jquery
    {#let dtree = layui.dtree#}
     $("#answer2").hide()
     $("#answer3").hide()
     $("#answer1").show()
     $("#A").show()
     $("#B").show()
     $("#C").show()
     $("#D").show()
    form.on('select(oneLeverChange)',
     function(data){
	 	var val=data.value;
        if(val ==1){
             $("#answer2").hide()
             $("#answer3").hide()
             $("#answer1").show()
             $("#A").show()
             $("#B").show()
             $("#C").show()
             $("#D").show()
         }
         if(val ==2){
             $("#answer2").show()
             $("#answer3").hide()
             $("#answer1").hide()
             $("#A").show()
             $("#B").show()
             $("#C").show()
             $("#D").show()
         }
         if(val ==3){
             $("#answer2").hide()
             $("#answer3").show()
             $("#answer1").hide()
             $("#A").hide()
             $("#B").hide()
             $("#C").hide()
             $("#D").hide()
             $("#A").hide()
             $("#B").hide()
             $("#C").hide()
             $("#D").hide()

         }
	});

    form.on('submit(dept-save)', function (data) {
        console.log(data.field,"合肥京东方家居服")
        if(data.field.title ==""){
            layer.msg("请填写标题");
            return  false
        }
        if(data.field.type ==1 || data.field.type ==2)
        if(data.field.A == "" || data.field.B == "" || data.field.C == "" || data.field.D == ""){
            layer.msg("请填写完善答案选项");
            return  false
        }
        var s ='';
        $('input[name="answer2"]:checked').each(function(){
          s+=$(this).val()+",";
        })
          var ne = s.replace(/[,，]$/, "");

        let option =data.field.A+","+data.field.B+","+data.field.C+","+data.field.D
        let datas =[]
        if(data.field.type ==1){
          datas.push({sort:data.field.sort,type: data.field.type,title: data.field.title,answer: data.field.answer1,option:option})
        }
       if(data.field.type ==2){
          datas.push({sort:data.field.sort,type: data.field.type,title: data.field.title,answer:ne,option:option})
        }
        if(data.field.type ==3){
          datas.push({sort:data.field.sort,type: data.field.type,title: data.field.title,answer: data.field.answer3,option:""})
        }

console.log(datas,"收到尽快发货带上飞机")
      $.ajax({
        url: '/admin/examination/save',
        data: JSON.stringify(datas[0]),
        dataType: 'json',
        contentType: 'application/json',
        type: 'post',
        success: function (result) {
          if (result.success) {
            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
              parent.layer.close(parent.layer.getFrameIndex(window.name))
                parent.layui.table.reload('user-table')
              parent.render()
            })
          } else {
            layer.msg(result.msg, { icon: 2, time: 1000 })
          }
        }
      })
      return false
    })
  })
</script>
<script>
</script>
</body>
</html>